<?php
namespace app\common\model;

class MemberAddress extends Base
{
    protected $resultSetType = 'collection';

    /**
     * [addAddress 添加地址]
     * @param [type] $data [description]
     */
	public function addAddress($data){
		if($this->isExist($data['uid'],$data['city'],$data['address'],$data['uname'],$data['mphone'])){
			$this->error='您已经添加过该地址';
			return false;
		}else{
			return $this->insert($data);
		}
	}

    /**
     * 条件查询单个
     * @return [type] [description]
     */
    public function selectFind($where=[]){
        return $this->where($where)->find();
    }

    /**
     * 更新地址
     * @return [type] [description]
     */
    public function saveOneData($data=[],$id=''){
        return $this->save($data,['id' => $id]);
    }

    /**
     * [setDefault 设为默认地址]
     * @param [type] $id  [地址ID]
     * @param [type] $uid [用户ID]
     */
    public function setDefault($id,$uid){
        if($this->selectFind(['id'=>$id,'uid'=>$uid])){
            //查询是否有默认地址
            $data = $this->isDefault($uid);
            if($data){
                $this->where(['id'=>$data['id']])->update(['default'=>0]);//将原默认地址设为0
            }
            $this->where(['id'=>$id])->update(['default'=>1]);
        }else{
            $this->error='地址不存在';
            return false;
        }
    }

    /**
     * [isExist 判断相同地址是否已经存在]
     * @param  [type]  $uid     [用户id]
     * @param  [type]  $city    [地址]
     * @param  [type]  $address [街道]
     * @param  [type]  $uname   [收货人]
     * @param  [type]  $mphone  [收货手机]
     * @return boolean          [description]
     */
    public function isExist($uid,$city,$address,$uname,$mphone){
    	return $this->selectFind(['uid'=>$uid,'city'=>$city,'address'=>$address,'uname'=>$uname,'mphone'=>$mphone]);
    }

    /**
     * [isDefault 是否默认]
     * @param  [type]  $uid [description]
     * @return boolean      [description]
     */
    public function isDefault($uid){
        return $this->selectFind(['uid'=>$uid,'default'=>1]);
    }

    /**
     * [memberAddress 会员地址]
     * @param  [type] $uid   [description]
     * @param  string $field [description]
     * @return [type]        [description]
     */
    public function memberAddress($uid,$field=''){
        $data = $this->where(['uid'=>$uid])->field($field)->order('id desc')->select()->toArray();
        $count = $this->where(['uid'=>$uid])->count();
        return [
            'count' => $count,
            'data' => $data
        ];
    }

    /**
     * 查询用户单个地址
     * @return [type] [description]
     */
    public function memberAddressFind($aid=''){
        $res = $this->where(['id'=>$aid])->field('id,city,uname,mphone,address,default,title')->find()-> toArray();
         return    self::setRemark($res);
    }

    /**
     * 处理字段数据
     * @param [type] $row [description]
     */
    public static function setRemark($row){

        if(isset($row['city'])){
            // 省
            $address_province = model('City')->findName(substr($row['city'], 0,strlen($row['city'])-4));
            // 市
            $address_city= model('City')->findName((substr($row['city'], 0,strlen($row['city'])-2)));
            // 县
            $address_county = model('City')->findName($row['city']);
            $row['city_name'] = $address_province.$address_city.$address_county;
        }
        return $row;
    }

}
